# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from pymongo import MongoClient

class TxmoviesPipeline:

    def __init__(self):
        client = MongoClient(host="localhost", port=27017)  # 连接本地MongoDB
        client.admin.authenticate("admin", "abc123456")  # 登录MongoDB数据库
        db = client['db_TXmovies']
        self.col = db['col_TXmovies']

    def process_item(self, item, spider):
        # print(item)
        # print(type(item))
        # 将item转换为字典 方便存储
        data = dict(item)
        print(data)
        # 数据持久化  插入 mongo数据库中
        # self.col.insert_one(data)
        return item
